﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.IRepository.SiteMgr;
using YunSpace.Domain.ViewModel.Sale;
using YunSpace.Domain.ViewModel.SiteMgr;
using YunSpace.Domain.ViewModel.SiteMgr.SalesTarget;
using YunSpace.Domain.ViewModel.SiteMgr.SalesTarget;
using YunSpace.Repository.SiteMgr;

namespace YunSpace.Application.SiteMgr
{
    public class SiteSalesApp
    {
        private readonly ISiteSalesTargetSettingRepository _siteSalesTargetSettingRepository = new SiteSalesTargetSettingRepository();

        #region 月目标设置

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="keyValue"></param>
        public bool SaveSiteSalesTargetSetting(SiteSalesTargetSettingEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                var firstModel = _siteSalesTargetSettingRepository.FindEntity(x => x.F_UserId == entity.F_UserId && x.F_DepartId == entity.F_DepartId && x.F_Year == entity.F_Year && x.F_Month == entity.F_Month && (x.F_DeleteMark == null || x.F_DeleteMark == false));

                if (firstModel != null)
                {
                    if (entity.F_IndicatorValue <= 0)
                    {
                        firstModel.Remove();
                        firstModel.F_IndicatorValue = 0;
                        _siteSalesTargetSettingRepository.Update(firstModel);
                        return true;
                    }
                    else
                    {
                        firstModel.Modify(firstModel.F_Id);
                        firstModel.F_IndicatorValue = entity.F_IndicatorValue;
                        _siteSalesTargetSettingRepository.Update(firstModel);
                        return true;
                    }
                }

                entity.Create();
                _siteSalesTargetSettingRepository.Insert(entity);
            }
            else
            {
                var firstEntity = _siteSalesTargetSettingRepository.FindEntity(keyValue);
                firstEntity.F_IndicatorValue = entity.F_IndicatorValue;
                firstEntity.F_DepartId = entity.F_DepartId;
                firstEntity.F_Month = entity.F_Month;
                firstEntity.F_Year = entity.F_Year;
                firstEntity.F_UserId = entity.F_UserId;

                firstEntity.Modify(keyValue);
                _siteSalesTargetSettingRepository.Update(firstEntity);
            }

            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public bool EditSiteSalesTargetSetting(string Uid, string Year, string Month, decimal Value)
        {
            if (!(String.IsNullOrWhiteSpace(Uid) || String.IsNullOrWhiteSpace(Year) || String.IsNullOrWhiteSpace(Month)))
            {
                var firstModel = _siteSalesTargetSettingRepository.FindEntity(x => x.F_UserId == Uid && x.F_Year == Year && x.F_Month == Month && (x.F_DeleteMark == null || x.F_DeleteMark == false));
                if (firstModel != null)
                {
                    firstModel.Modify(firstModel.F_Id);
                    firstModel.F_IndicatorValue = Value;
                    _siteSalesTargetSettingRepository.Update(firstModel);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        public bool DelSiteSaleTargetSetting(string Uid, string Year)
        {
            if (!(String.IsNullOrWhiteSpace(Uid) || String.IsNullOrWhiteSpace(Year)))
            {
               var list= _siteSalesTargetSettingRepository.FindList($@"select * from Site_Sales_TargetSetting s where s.F_Year={Year} and s.F_UserId='{Uid}' and (s.F_DeleteMark IS NULL || s.F_DeleteMark = 0)");
                if (list.Count() > 0)
                {
                    foreach (var item in list)
                    {
                        item.Remove();
                        _siteSalesTargetSettingRepository.Update(item);
                    }
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<MonthTargetGridDto> GetMonthTargetGridDtos(Pagination pagination, GetMonthTargetGridRequest request)
        {
            return _siteSalesTargetSettingRepository.GetMonthTargetGridDtos(pagination, request);
        }

        /// <summary>
        /// 获取汇总的目标值
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public decimal GetThresholdsBy(GetMonthTargetGridRequest request)
        {
            return _siteSalesTargetSettingRepository.GetThresholdsBy(request);
        }

        #endregion 月目标设置

        #region 目标查看

        /// <summary>
        /// 获取目标查看列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<LookTargetViewModel> GetLookTargetViewModels(Pagination pagination,
            GetLookTargetGridJsonRequest request)
        {
            return _siteSalesTargetSettingRepository.GetLookTargetDtos(pagination, request).Select(x =>
                new LookTargetViewModel()
                {
                    DealDemandCount = x.DealDemandCount,
                    DemandCount = x.DemandCount,
                    ReturnedAmount = x.ReturnedAmount,
                    ReturnTargetAmount = x.ReturnTargetAmount,
                    Seller = x.UserName,
                    SellerId = x.UserId,
                    SignedAmount = x.SignedAmount,
                    ThresholdAmount = x.ThresholdAmount
                }).ToList();
        }

        #endregion 目标查看





        #region 废弃

        ///// <summary>
        ///// 获取目标查看列表数据
        ///// </summary>
        ///// <returns></returns>
        //public List<LookTargetViewModel> GetLookTargetViewModels(Pagination pagination, GetLookTargetGridJsonRequest request)
        //{
        //    var data1 = _siteSalesTargetSettingRepository.GetTargetAchievingRateOfLookTargetDtos(request);
        //    var data2 = _siteSalesTargetSettingRepository.GetReturnedAchievingRateOfLookTargetDtos(request);
        //    var data3 = _siteSalesTargetSettingRepository.GetConversionRateOfLookTargetDtos(request);

        //    var vms = new List<LookTargetViewModel>();

        //    foreach (var dto in data1)
        //    {
        //        var vm = new LookTargetViewModel
        //        {
        //            F_UserId = dto.F_UserId,
        //            F_Seller = dto.F_Seller,
        //            F_IndicatorValues = dto.F_IndicatorValues,
        //            F_SignedValues = dto.F_SignedValues
        //        };

        //        if (data2.Any(x => x.F_UserId == dto.F_UserId))
        //        {
        //            var firstDto = data2.First(x => x.F_UserId == dto.F_UserId);
        //            vm.F_ReturnTargetValues = firstDto.F_ReturnTargetValues;
        //            vm.F_ReturnedValues = firstDto.F_ReturnedValues;
        //        }

        //        if (data3.Any(x => x.F_UserId == dto.F_UserId))
        //        {
        //            var firstDto = data3.First(x => x.F_UserId == dto.F_UserId);
        //            vm.DemandCount = firstDto.DemandCount;
        //            vm.DealCount = firstDto.DealCount;
        //        }

        //        vms.Add(vm);
        //    }

        //    pagination.records = vms.Count;
        //    vms = vms.Skip((pagination.page - 1) * pagination.rows).Take(pagination.rows).ToList();

        //    return vms;
        //}

        #endregion


    }
}
